<?php
// +----------------------------------------------------------------------
// | [KyPHP System] Copyright (c) 2020 http://www.kuryun.com/
// +----------------------------------------------------------------------
// | [KyPHP] 并不是自由软件,你可免费使用,未经许可不能去掉KyPHP相关版权
// +----------------------------------------------------------------------
// | Author: fudaoji <fdj@kuryun.cn>
// +----------------------------------------------------------------------
/**
 * Created by PhpStorm.
 * Script Name: Notice.php
 * Create: 2020/7/16 17:21
 * Description: 系统公告
 * Author: fudaoji<fdj@kuryun.cn>
 */

namespace app\admin\controller;

class Notice extends Base
{
    private $noticeM;
    public function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
        $this->noticeM = model('notice');
    }

    /**
     * 保存数据
     * Author: fudaoji<fdj@kuryun.cn>
     */
    public function savePost(){
        if(request()->isPost()){
            $post_data = input('post.');
            $res = $this->validate($post_data, 'Notice.' . (isset($post_data['id']) ? 'edit' : 'add'));
            if($res !== true){
                $this->error($res, '', ['token' => request()->token()]);
            }
            $post_data['publish_time'] = strtotime($post_data['publish_time']);
            if(empty($post_data['id'])){
                $this->noticeM->addOne($post_data);
            }else{
                $this->noticeM->updateOne($post_data);
            }
            $this->success('操作成功', url('index'));
        }
    }

    /**
     * 系统公告
     * @return mixed
     * Author: fudaoji<fdj@kuryun.cn>
     */
    public function index(){
        $data_list = $this->noticeM->page($this->pageSize, [], ['id' => 'desc'],true, 1);
        $page = $data_list->render();
        return $this->show(['data_list' => $data_list, 'page' => $page]);
    }

    /**
     * 发布系统公告
     * @return mixed
     * Author: fudaoji<fdj@kuryun.cn>
     */
    public function add(){
        $builder = new FormBuilder();
        $builder->setPostUrl(url('savePost'))
            ->addFormItem('title', 'text', '标题', '不超过50字', [], 'required maxlength=80')
            ->addFormItem('publish_time', 'datetime', '发布时间', '发布时间', [], 'required')
            ->addFormItem('content', 'ueditor', '内容', '内容', [], 'required');
        return $builder->show();
    }

    /**
     * 编辑系统公告
     * @return mixed
     * Author: fudaoji<fdj@kuryun.cn>
     */
    public function edit(){
        $data = $this->noticeM->getOne(input('id', 0));
        if(empty($data)){
            $this->error('数据不存在');
        }
        $data['publish_time'] = date('Y-m-d H:i:s', $data['publish_time']);
        $builder = new FormBuilder();
        $builder->setPostUrl(url('savePost'))
            ->addFormItem('id', 'hidden', 'id', 'id')
            ->addFormItem('title', 'text', '标题', '不超过50字', [], 'required maxlength=80')
            ->addFormItem('publish_time', 'datetime', '发布时间', '发布时间', [], 'required')
            ->addFormItem('status', 'radio', '状态', '状态', [1 => '正常', 0 => '禁用'], 'required')
            ->addFormItem('content', 'ueditor', '内容', '内容', [], 'required')
            ->setFormData($data);
        return $builder->show();
    }
}